package com.gcb.controller.order;

import com.gcb.common.annotation.Log;
import com.gcb.common.core.domain.AjaxResult;
import com.gcb.common.core.page.TableDataInfo;
import com.gcb.common.enums.BusinessType;
import com.gcb.common.utils.poi.ExcelUtil;
import com.gcb.controller.BaseController;
import com.gcb.domain.dto.OrderProjectQueryDTO;
import com.gcb.domain.dto.OrderProjectSaveDTO;
import com.gcb.domain.vo.OrderProjectVO;
import com.gcb.service.IOrderProjectService;
import com.gcb.framework.utils.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 * 项目订单管理
 */
@RestController
@RequestMapping("/orderProject")
@Tag(name = "后台项目订单管理", description = "后台项目订单管理")
public class OrderProjectController extends BaseController {

    @Resource
    private IOrderProjectService orderProjectService;

    /**
     * 发布项目订单
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderProject:add')")
    @PostMapping("/add")
    @Operation(summary = "801.发布项目订单")
    public AjaxResult add(@Valid @RequestBody OrderProjectSaveDTO dto) {
        orderProjectService.add(dto,  SecurityUtils.getUserId());
        return success();
    }

    /**
     * 修改项目订单
     *
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderProject:update')")
    @PostMapping("/update")
    @Operation(summary = "802.修改项目订单")
    public AjaxResult update(@Valid @RequestBody OrderProjectSaveDTO dto) {
        orderProjectService.update(dto, SecurityUtils.getUserId());
        return success();
    }

    /**
     * 项目订单列表
     *
     * @param queryDTO
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderProject:pageList')")
    @GetMapping("/pageList")
    @Operation(summary = "803.项目订单列表")
    public TableDataInfo pageList(OrderProjectQueryDTO queryDTO) {
        startPage();
        List<OrderProjectVO> list = orderProjectService.list(queryDTO);
        return getDataTable(list);
    }

    /**
     * 项目订单详情
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderProject:getDetail')")
    @GetMapping("/getDetail")
    @Operation(summary = "804.项目订单详情")
    public AjaxResult detail(Long id) {
        OrderProjectVO vo = orderProjectService.getDetail(id, null, null);
        return success(vo);
    }

    /**
     * 批量修改项目订单状态
     *
     * @param ids
     * @param state
     * @return
     */
    @PreAuthorize("@ss.hasPermi('order:orderProject:batchUpdateState')")
    @GetMapping("/batchUpdateState")
    @Operation(summary = "805.批量修改项目订单状态")
    public AjaxResult batchUpdateState(Long[] ids, Integer state) {
        orderProjectService.batchUpdateState(Arrays.asList(ids), state);
        return success();
    }

    /**
     * 项目订单管理导出
     */
    @PreAuthorize("@ss.hasPermi('order:orderProject:export')")
    @Log(title = "项目订单管理导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @Operation(summary = "806.项目订单管理导出")
    public void export(HttpServletResponse response, OrderProjectQueryDTO queryDTO) {
        List<OrderProjectVO> list = orderProjectService.list(queryDTO);
        ExcelUtil<OrderProjectVO> util = new ExcelUtil<>(OrderProjectVO.class);
        util.exportExcel(response, list, "项目订单管理导出");
    }
}
